import json
from urllib import parse

import requests
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from Database import Database
from util import unique_id
import time

db = Database()
video_params = ('id','title','description','av_id','bv_id','create_time','cover')
keyword_video_params = ('keyword_id','video_id','create_time')
tbl_video = 'dc_videos'
tbl_keyword_video = 'dc_keyword_video'
tbl_keyword = 'dc_video_keywords'
interval = 3
while True:
    keywords = db.select_more(tbl_keyword, 'status=0')
    for val in keywords:
        key_word = val['keyword']
        keyword_id = val['id']
        db.update(tbl_keyword, {"status": "1"}, f"id={keyword_id}")
        keyword_url = parse.quote(key_word, encoding="gbk")
        headers = {
            "accept":"application/json, text/plain, */*",
            "accept-encoding":"gzip, deflate, br",
            "accept-language":"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,it;q=0.6",
            "cookie":"_uuid=4AF3D6F2-F5E0-AF00-A053-AB635E9892E777932infoc; buvid3=3653B286-711C-416D-8356-C49D570154A3167612infoc; rpdid=|(umukkYlY|)0J'uYJR|ml~|l; LIVE_BUVID=AUTO3916347908636381; video_page_version=v_old_home; blackside_state=0; i-wanna-go-back=-1; CURRENT_BLACKGAP=0; buvid4=6341142E-B732-3B21-7ADA-37BC1B4EF70717081-022012812-x/szqScNSPOoi07CZGUSmg%3D%3D; buvid_fp_plain=undefined; PVID=1; CURRENT_QUALITY=80; b_ut=5; buvid_fp=1d15bb18711b98edd1050af03c392076; DedeUserID=54354250; DedeUserID__ckMd5=55a3c70af9e0e0ea; nostalgia_conf=-1; hit-dyn-v2=1; fingerprint3=0a79e5407efa61a09c86584d0f63dd15; fingerprint=43a23ffda572fbb1e7d0c43efbe1f29f; bp_video_offset_54354250=684128843854774300; SESSDATA=116faf79%2C1674295179%2Ca452e%2A71; bili_jct=3b216a195618b0c561a5a78f64cdc46f; sid=8r0x8cp0; bsource=search_baidu; CURRENT_FNVAL=80; innersign=0; b_lsid=56E572C8_18243FC1A15; b_timer=%7B%22ffp%22%3A%7B%22333.1007.fp.risk_3653B286%22%3A%2218243FC213A%22%2C%22333.337.fp.risk_3653B286%22%3A%2218243FC4035%22%2C%22333.788.fp.risk_3653B286%22%3A%221823EED55F4%22%2C%22333.976.fp.risk_3653B286%22%3A%221823D544326%22%7D%7D",
            "origin":"https://search.bilibili.com",
            "referer":f"https://search.bilibili.com/video?vt=97991161&keyword={keyword_url}",
            "sec-ch-ua":'.Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
            "sec-ch-ua-mobile":"?0",
            "sec-ch-ua-platform":"'Windows'",
            "sec-fetch-dest":"empty",
            "sec-fetch-mode":"cors",
            "sec-fetch-site":"same-site",
            "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
        }
        page = 1
        page_size = 42
        url = f"https://api.bilibili.com/x/web-interface/search/type?__refresh__=true&_extra=&context=&page={page}&page_size={page_size}&from_source=&from_spmid=333.337&platform=pc&highlight=1&single_column=0&keyword={key_word}&category_id=&search_type=video&dynamic_offset=0&preload=true&com2co=true"
        requests.session()
        req = requests.get(url,headers=headers)
        data = json.loads(req.content)
        if 0 == data['code']:
            result = data['data']['result']
            video_list = []
            video_list_tuple = []
            keyword_video_list = []
            keyword_video_list_tuple = []
            cur_time = int(time.time())
            for item in result:
                video_info = db.select_one(tbl_video, f"av_id={item['aid']}")
                if video_info:
                    video_id = str(video_info['id'])
                else:
                    video_id = unique_id()
                    video_item = {
                        'id':str(video_id),
                        'title':str(item['title']),
                        'description':str(item['description']),
                        'av_id':str(item['id']),
                        'bv_id':str(item['bvid']),
                        'create_time':str(cur_time),
                        'cover':str(item['pic']),
                    }
                    video_list.append(video_item)
                    video_list_tuple.append(tuple(video_item.values()))
                if not db.select_one(tbl_keyword_video, f"video_id={video_id} AND keyword_id={keyword_id}"):
                    keyword_video_item = {
                        'keyword_id':str(keyword_id),
                        'video_id':str(video_id),
                        'create_time':str(cur_time)
                    }
                    keyword_video_list.append(keyword_video_item)
                    keyword_video_list_tuple.append(tuple(keyword_video_item.values()))
            if keyword_video_list:
                db.insert_many(tbl_keyword_video, params=keyword_video_params,all_data=tuple(keyword_video_list_tuple))
            if video_list:
                db.insert_many(tbl_video, params=video_params,all_data=tuple(video_list_tuple))
            db.update(tbl_keyword, {"status": "2","finish_time":int(time.time())}, f"id={keyword_id}")
            print(f'[{key_word}]搜索结束,等待{interval}秒')
            time.sleep(interval)